package io.finer.erp.jeecg.purchase.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.finer.erp.jeecg.purchase.entity.*;
import io.finer.erp.jeecg.purchase.mapper.PurchaseRequestLineMapper;
import io.finer.erp.jeecg.purchase.mapper.PurchaseRequestMapper;
import io.finer.erp.jeecg.purchase.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.finer.erp.jeecg.purchase.vo.PurchaseRequestLinePage;
import io.finer.erp.jeecg.purchase.vo.PurchaseRequestPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.math.BigDecimal;
import java.sql.Array;
import java.util.*;

/**
 * <p>
 * 采购申请行 服务实现类
 * </p>
 *
 * @author yaoyifan
 * @since 2021-04-12
 */
@Service
public class PurchaseRequestLineServiceImpl extends ServiceImpl<PurchaseRequestLineMapper, PurchaseRequestLine> implements PurchaseRequestLineService {

    @Autowired
    PurchaseRequestService purchaseRequestService;

    @Autowired
    PurchaseSettingsService purchaseSettingsService;

    @Autowired
    PurchaseOrderService purchaseOrderService;

    @Autowired
    PurchaseOrderLineService purchaseOrderLineService;

    @Override
    public List<PurchaseRequestLinePage> selectPurchaseRequestLinePageByMid(String mid) {
        return this.baseMapper.selectPurchaseRequestLinePageByMid(mid);
    }

    @Override
    public IPage<PurchaseRequestPage> selectPurchaseRequestLinePage(Page page, Wrapper<PurchaseRequestLinePage> queryWrapper) {
        return this.baseMapper.selectPurchaseRequestLinePage(page, queryWrapper);
    }


    @Override
    public List<String> createPurchaseOrder(Collection<? extends Serializable> idList) {
        ArrayList<String> poIds = new ArrayList<>();
        List<PurchaseRequestLine> purchaseRequestLines = listByIds(idList);
        HashMap<String, List<PurchaseOrderLine>> pos = new HashMap<>();
        for (PurchaseRequestLine purchaseRequestLine: purchaseRequestLines){
            PurchaseOrderLine purchaseOrderLine = new PurchaseOrderLine();
            purchaseOrderLine.setPrLine(purchaseRequestLine.getId());
            purchaseOrderLine.setMaterialId(purchaseRequestLine.getMaterialId());
            purchaseOrderLine.setDestWarehouseId(purchaseRequestLine.getDestWarehouseId());

            BigDecimal price = purchaseRequestLine.getPrice();
            BigDecimal refPrice = purchaseRequestLine.getReferencePrice();
            BigDecimal resPrice = price.compareTo(BigDecimal.valueOf(0.00)) == 0 ? refPrice : price;
            purchaseOrderLine.setPrice(resPrice);
            purchaseOrderLine.setQty(purchaseRequestLine.getProcessNum());
            purchaseOrderLine.setLineAmount(purchaseRequestLine.getProcessNum().multiply(resPrice));
            PurchaseRequest purchaseRequest = purchaseRequestService.getById(purchaseRequestLine.getMid());
            purchaseOrderLine.setApplicant(purchaseRequest.getRequestUserId());
            purchaseOrderLine.setSysOrgCode(purchaseRequest.getSysOrgCode());
            String key = purchaseRequestLine.getSupplierId() + "-" + purchaseRequestLine.getBuyer();
            if(pos.containsKey(key)){
                pos.get(key).add(purchaseOrderLine);
            }else{
                ArrayList<PurchaseOrderLine> purchaseOrderLines = new ArrayList<>();
                purchaseOrderLines.add(purchaseOrderLine);
                pos.put(key, purchaseOrderLines);
            }
        }
        PurchaseSettings purchaseSettings = purchaseSettingsService.getOne(new QueryWrapper<PurchaseSettings>());
        pos.forEach((k, pols) -> {
            String[] ks = k.split("-");
            String supplierId = ks[0];
            String buyer = ks[1];
            PurchaseOrder purchaseOrder = new PurchaseOrder();
            purchaseOrder.setSupplierId(supplierId);
            purchaseOrder.setBuyer(buyer);
            purchaseOrder.setBillDate(new Date());
            purchaseOrder.setSource("PR");
            purchaseOrder.setDestShipAddressId(purchaseSettings.getDefaultReceivingAddressId());
            purchaseOrder.setDestBillAddressId(purchaseSettings.getDefaultBillingAddressId());
            purchaseOrder.setBillAddressId(purchaseSettings.getDefaultBillingAddressId());
            purchaseOrderService.saveMain(purchaseOrder, pols);
            poIds.add(purchaseOrder.getId());
        });

        return poIds;
    }
}
